package com.huanbao.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huanbao.base.dto.VolunteerApplyCheckDTO;
import com.huanbao.entity.User;
import com.huanbao.entity.VolunteerApply;
import com.huanbao.mapper.VolunteerApplyMapper;
import com.huanbao.service.UserService;
import com.huanbao.service.VolunteerApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 志愿者申请表 服务实现类
 * </p>
 *
 * @author zq
 * @since 2021-04-27
 */
@Service
public class VolunteerApplyServiceImpl extends ServiceImpl<VolunteerApplyMapper, VolunteerApply> implements VolunteerApplyService {

    @Resource
    private VolunteerApplyMapper volunteerApplyMapper;

    @Autowired
    private UserService userService;

    @Override
    @Transactional
    public void check(VolunteerApplyCheckDTO checkDTO) {
        VolunteerApply volunteerApply = volunteerApplyMapper.selectById(checkDTO.getVolunteerApplyId());
        volunteerApply.setState(checkDTO.getResult());
        volunteerApplyMapper.updateById(volunteerApply);
        if (checkDTO.getResult() == 1) {
            userService.createMessage("志愿者申请通过，重新登录即可拥有志愿者相关功能。", volunteerApply.getUserId());
            User id = userService.getById(volunteerApply.getUserId());
            id.setType(2);
            userService.updateById(id);
        } else {
            userService.createMessage("志愿者申请不通过。", volunteerApply.getUserId());
        }
    }
}
